import { _decorator, Button, Tween, tween, UITransform } from 'cc';
import { BaseUI } from '../../../../frame/component/ui.base';
import GameDC from '../../../data/data.game.center';
import DataManager from '../../../manager/data.manager';
import { Float } from '../float';
import { Popup } from '../pop-up';
const { ccclass, property } = _decorator;

@ccclass('UIHomeBottom')
export class UIHomeBottom extends BaseUI {
	private uitrans: UITransform;
	private vecasit: mtec.cc.VecAssist;

	private ui_status: 'enter' | 'leave';

	protected initAfterOnLoad(){
		this.init();
	}

	protected start(){
		this.adaptUI();
		this.node.active = false;
	}

	private init(){
		this.uitrans = this.node.getComponent(UITransform);
		this.vecasit = new mtec.cc.VecAssist(this.node);
	}

	private adaptUI(){
		this.vecasit.getPosition('center', 'over_bottom');
		this.node.setPosition(this.vecasit.vec);
	}

	protected _click_event_: { [name: string]: (this: UIHomeBottom, button: Button) => void; } = {
		async stash(btn){
			if(GameDC.Status.home_ui_btn_lock){
				return void 0;
			}
			GameDC.Status.home_ui_btn_lock = true;

			let count = GameDC.Status.prop_stash;
			if(count>0){
				GameDC.Trigger.STASH = false;
			}else{
				let list = DataManager.prop_get;
				if(!list.includes('stash')){
					GameDC.Trigger.HILIT_DIAMOND = true;
					let reap_count = await Popup.Prop('stash');
					GameDC.Trigger.HILIT_DIAMOND = false;
					if(reap_count>0){
						DataManager.prop_get.push('stash');
						count += reap_count;
						Float.moveProp('stash', btn.node.getChildByName('icon'));
						GameDC.Status.prop_stash = count;

						Float.Hint('Get props');
					}
				}else{
					Float.Hint('Used once!');
				}
			}

			GameDC.Status.home_ui_btn_lock = false;
		},
		async revoke(btn){
			if(GameDC.Status.home_ui_btn_lock){
				return void 0;
			}
			GameDC.Status.home_ui_btn_lock = true;

			let count = GameDC.Status.prop_revoke;
			if(count>0){
				GameDC.Trigger.REVOKE = false;
			}else{
				let list = DataManager.prop_get;
				if(!list.includes('revoke')){
					GameDC.Trigger.HILIT_DIAMOND = true;
					let reap_count = await Popup.Prop('revoke');
					GameDC.Trigger.HILIT_DIAMOND = false;
					if(reap_count>0){
						DataManager.prop_get.push('revoke');
						count += reap_count;
						Float.moveProp('revoke', btn.node.getChildByName('icon'));
						GameDC.Status.prop_revoke = count;

						Float.Hint('Get props');
					}
				}else{
					Float.Hint('Used once!');
				}
			}

			GameDC.Status.home_ui_btn_lock = false;
		},
		async shuffle(btn){
			if(GameDC.Status.home_ui_btn_lock){
				return void 0;
			}
			GameDC.Status.home_ui_btn_lock = true;

			let count = GameDC.Status.prop_shuffle;
			if(count>0){
				GameDC.Trigger.SHUFFLE = false;
			}else{
				let list = DataManager.prop_get;
				if(!list.includes('shuffle')){
					GameDC.Trigger.HILIT_DIAMOND = true;
					let reap_count = await Popup.Prop('shuffle');
					GameDC.Trigger.HILIT_DIAMOND = false;
					if(reap_count>0){
						DataManager.prop_get.push('shuffle');
						count += reap_count;
						Float.moveProp('shuffle', btn.node.getChildByName('icon'));
						GameDC.Status.prop_shuffle = count;

						Float.Hint('Get props');
					}
				}else{
					Float.Hint('Used once!');
				}
			}

			GameDC.Status.home_ui_btn_lock = false;
		}
	}

	protected listening(){
		let list = [
			this.follow(GameDC.Trigger, 'UIHome', (o, v)=>this.onUIHomeTrigger(o, v), false),
		];
		return list;
	}

	private onUIHomeTrigger(ov: boolean, show: boolean){
		if(ov===show){
			return void 0;
		}

		if(show && this.ui_status!=='enter'){
			this.ui_status = 'enter';
			this.enter();
		}else if(this.ui_status!=='leave'){
			this.ui_status = 'leave';
			this.leave();
		}
	}

	private enter(){
		this.node.active = true;

		this.vecasit.getPosition('center', 'bottom');

		Tween.stopAllByTarget(this.node);
		tween(this.node)
		.to(0.5, {position: this.vecasit.vec}, {easing: 'cubicOut'})
		.start();
	}

	private leave(){
		this.vecasit.getPosition('center', 'over_bottom');

		Tween.stopAllByTarget(this.node);
		tween(this.node)
		.to(0.5, {position: this.vecasit.vec}, {easing: 'cubicOut'})
		.call(()=>{
			this.node.active=false;
		}).start();
	}
}

